<?php
namespace app\admin\controller;
use think\Db;
use app\common\controller\Backend;
class Jishu extends Backend{
	public function flist(){
		$list1=model('AppCategory')->where('develop_model', 1)->paginate(8);
		$list2=model('AppCategory')->where('develop_model', 2)->paginate(8);
		$this->assign('list1',$list1);
		$this->assign('list2',$list2);
		return $this->view->fetch();
	}
	public function fedit(){
		$version=model('AppVersion')->select();
		/*dump($version);
		die();*/
        $id=input('get.id');
		$data=array();
		if($id){
          $data=model('AppCategory')->where('id', $id)->find();
          $data['version_id']=explode(',', $data['version_id']);
		}
		$this->assign('data',$data);
		$this->assign('versions',$version);
		return $this->view->fetch();
	}

    public function fsave(){
    	$data=$this->request->post();
    	$data['version_id']=implode(",", $data['version_id']);
    	$model=model('AppCategory');
    	if(empty($data['id'])){
           $model->data($data);
    	   $re=$model->save();
    	}else{
    	   $re=$model->save($data,['id' => $data['id']]);
    	}
    	if($re!==false){
    		$this->success(__('编辑成功'), 'jishu/flist');
    	}else{
    		$this->success(__('编辑失败'), 'jishu/flist');
    	}
    } 

    public function fdelete(){
    	$id=input('get.id');
    	$re=model('AppCategory')->destroy($id);
        if($re){
    		$this->success(__('删除成功'), 'jishu/flist');
    	}else{
    		$this->success(__('删除失败'), 'jishu/flist');
    	}
    }



	public function klist(){
        //独立服务
        $list1=model('DevPrice')->where(['develop_model'=>1])->paginate(8);;

		//单一服务的
		$cate2=model('AppCategory')->where('develop_model', 2)->field('id,name')->select();
        $list2=array();
		foreach ($cate2 as $key => $value) {
			$list=array();
            //独立部分
			$list_type1=model('DevPrice')->where(['develop_model'=>2, 'cate_id'=>$value['id'],'type'=>1])->select();
            //通用部分
			$list_type2=model('DevPrice')->where(['develop_model'=>2, 'cate_id'=>$value['id'],'type'=>2])->select();

    
            if($list_type1) {
                $list[1]= collection($list_type1)->toArray();
            }  

            if($list_type2) {
                $list[2]= collection($list_type2)->toArray();
            }  
   
			if(!empty($list)){
				$list2[$value['id']][]=$list;
				$list2[$value['id']]['name']=$value['name'];
			}
			
		}

		$this->assign('list1',$list1);
        $this->assign('list2',$list2);
		return $this->view->fetch();
	}
	public function kedit(){
		//应用版本
		$version=model('AppVersion')->select();
        
        //应用范畴
        $list1=model('AppCategory')->where('develop_model', 1)->paginate(8);
		$list2=model('AppCategory')->where('develop_model', 2)->paginate(8);
		$this->assign('list1',$list1);
		$this->assign('list2',$list2);

		$id=input('get.id');
		$data=array();
		if($id){
          $data=model('DevPrice')->where('id', $id)->find();
          $data['version_price']=json_decode($data['version_price'],true);
          $list = array();
          array_map(function($value) use (&$list){
              $list[$value['0']] = $value['1'];
          },  $data['version_price']);
          $this->assign('list',$list);
		}
		$this->assign('data',$data);

		$this->assign('versions',$version);
		return $this->view->fetch();
	}

    public function ksave(){
    	$data=$this->request->post();
    	$model=model('DevPrice');
    	if(empty($data['id'])){
           $model->data($data);
    	   $re=$model->save();
    	}else{
    	   $re=$model->save($data,['id' => $data['id']]);
    	}
    	if($re){
    		$this->success(__('编辑成功'), 'jishu/klist',true);
    	}else{
    		$this->success(__('编辑失败'), 'jishu/klist',true);
    	}
    } 

     public function kdelete(){
    	$id=input('get.id');
    	$re=model('DevPrice')->destroy($id);
        if($re){
    		$this->success(__('删除成功'), 'jishu/klist');
    	}else{
    		$this->success(__('删除失败'), 'jishu/klist');
    	}
    }


   //应用语言
	public function ylist(){
		$list=model('AppLanguage')->paginate(16);
	    $this->assign('list',$list);
		return $this->view->fetch();
	}
	public function yedit(){
		//技术语言选项
		$tech=model('TechnologyLanguage')->select();
	    $this->assign('tech',$tech);

        //应用范畴-系统服务
        $cate1=model('AppCategory')->where('develop_model', 1)->select();
        $this->assign('cate1',$cate1);

        $id=input('get.id');
		$data=array();
		if($id){
          $data=model('AppLanguage')->where('id', $id)->find();
          $data['tech_id']=explode(',', $data['tech_id']);
		}
		$this->assign('data',$data);

		return $this->view->fetch();
	}

	public function ysave(){
    	$data=$this->request->post();
    	$model=model('AppLanguage');
    	$data['tech_id']=implode(',', $data['tech_id']);
    	if(empty($data['id'])){
           $model->data($data);
    	   $model->save();
    	}else{
    		$model->save($data,['id' => $data['id']]);
    	}
    	if($model->id){
    		$this->success(__('添加成功'), 'jishu/ylist');
    	}else{
    		$this->success(__('添加失败'), 'jishu/ylist');
    	}
    }

    public function ydelete(){
        $id=input('get.id');
        $re=model('AppLanguage')->destroy($id);
        if($re){
            $this->success(__('删除成功'), 'jishu/ylist');
        }else{
            $this->success(__('删除失败'), 'jishu/ylist');
        }
    }


    //技术语言
	public function jlist(){
		$list=model('TechnologyLanguage')->paginate(8);
	    $this->assign('list',$list);
		return $this->view->fetch();
	}
	public function jedit(){
		$id=input('get.id');
		$data=array();
		if($id){
          $data=model('TechnologyLanguage')->where('id', $id)->find();
		}
		$this->assign('data',$data);
		return $this->view->fetch();
	}

    public function jsave(){
    	$data=$this->request->post();
    	$model=model('TechnologyLanguage');
    	if(empty($data['id'])){
           $model->data($data);
    	   $model->save();
    	}else{
    		$model->save($data,['id' => $data['id']]);
    	}
    	if($model->id){
    		$this->success(__('添加成功'), 'jishu/jlist');
    	}else{
    		$this->success(__('添加失败'), 'jishu/jlist');
    	}
    }

    public function jdelete(){
        $id=input('get.id');
        $re=model('TechnologyLanguage')->destroy($id);
        if($re){
            $this->success(__('删除成功'), 'jishu/jlist');
        }else{
            $this->success(__('删除失败'), 'jishu/jlist');
        }
    }




	//应用版本
	public function blist(){
		$versions=model('AppVersion')->paginate(10);
	    $this->assign('versions',$versions);
		return $this->view->fetch();
	}
	public function bedit(){
		$id=input('get.id');
		$data=array();
		if($id){
          $data=model('AppVersion')->where('id', $id)->find();
		}
		$this->assign('data',$data);
		return $this->view->fetch();
	}

    public function bsave(){
    	$data=$this->request->post();
    	$data['terminal']=implode("", $data['terminal']);
    	$version=model('AppVersion');
    	if(empty($data['id'])){
           $version->data($data);
    	   $version->save();
    	}else{
    		$version->save($data,['id' => $data['id']]);
    	}
    	if($version->id){
    		$this->success(__('添加成功'), 'jishu/blist');
    	}else{
    		$this->success(__('添加失败'), 'jishu/blist');
    	}
    } 

    public function bdelete(){
    	$id=input('get.id');
    	$re=model('AppVersion')->destroy($id);
        if($re){
    		$this->success(__('删除成功'), 'jishu/blist');
    	}else{
    		$this->success(__('删除失败'), 'jishu/blist');
    	}
    }



    //应用类型
	public function llist(){
		$list=model('AppType')->paginate(10);
	    $this->assign('list',$list);
		return $this->view->fetch();
	}
	public function ledit(){
        $version=model('AppVersion')->select();
		/*dump($version);
		die();*/
        $id=input('get.id');
		$data=array();
		if($id){
          $data=model('AppType')->where('id', $id)->find();
          $data['version_id']=explode(',', $data['version_id']);
		}
		$this->assign('data',$data);
		$this->assign('versions',$version);
		return $this->view->fetch();
	}

	public function lsave(){
    	$data=$this->request->post();
    	$data['version_id']=implode(",", $data['version_id']);
    	$model=model('AppType');
    	if(empty($data['id'])){
           $model->data($data);
    	   $re=$model->save();
    	}else{
    	   $re=$model->save($data,['id' => $data['id']]);
    	}
    	if($re!==false){
    		$this->success(__('编辑成功'), 'jishu/llist');
    	}else{
    		$this->success(__('编辑失败'), 'jishu/llist');
    	}
    } 


    public function ldelete(){
    	$id=input('get.id');
    	$re=model('AppType')->destroy($id);
        if($re){
    		$this->success(__('删除成功'), 'jishu/llist');
    	}else{
    		$this->success(__('删除失败'), 'jishu/llist');
    	}
    }





   //应用行业
	public function hlist(){
        $list=Db::table('member_app_industry')->paginate(10);
        $this->assign('list',$list);
		return $this->view->fetch();
	}

	public function hedit(){
		$plate=Db::table('member_plate')->where(['parent'=>0])->select();


		//子板块
		foreach ($plate as $key => &$value) {
			$value['child']=Db::table('member_plate')->where(['parent'=>$value['id']])->select();
            $chid_id= array_column($value['child'], 'id');
            if(empty($chid_id)){
                $chid_id[]=$value['id'];
            }
            $value['page_count']=Db::table('member_page')->where(['plate_id'=>array('in',$chid_id)])->count();

            $value['nochild_page_count']=Db::table('member_page')->where(['plate_id'=>$value['id']])->count();
            if(!empty($value['child']) && $value['nochild_page_count']>0){
                $value['child'][]="无";
                $value['page_count']=$value['page_count']+$value['nochild_page_count'];
            }
		}
       /* dump($plate);
        die();*/
		
		//逻辑
        $logic=Db::table('member_app_logic')->select();

        $id=input('get.id');
		$data=array();
		if($id){
           $data=Db::table('member_app_industry')->where('id', $id)->find();
           $data['bid']=explode(',', $data['bid']);
           $data['child_bid']=explode(',', $data['child_bid']);
           $data['page_id']=explode(',', $data['page_id']);
           $data['gong_id']=explode(',', $data['gong_id']);
		}
        /* dump($data);
        die();*/
		$this->assign('data',$data);
		$this->assign('plate',$plate);
		$this->assign('logic',$logic);
		return $this->view->fetch();
	}


    public function hsave(){
        $data=$this->request->post();

        if(empty($data['bid'])){
             $this->error(__('请选择板块'), 'jishu/hlist');
        }
       /* if(empty($data['child_bid'])){
             $this->error(__('请选择子板块'), 'jishu/hlist');
        }*/
         if(empty($data['page_id'])){
             $this->error(__('请选择页面'), 'jishu/hlist');
        }
        $data['bid']=implode(',', $data['bid']);
        $data['child_bid']=empty($data['child_bid'])?'':implode(',', $data['child_bid']);
        $data['page_id']=implode(',', $data['page_id']);
        isset($data['gong_id'])?$data['gong_id']=implode(',', $data['gong_id']):'';
        if(empty($data['id'])){
            $re=Db::table('member_app_industry')->insert($data);
        }else{
            $re=Db::table('member_app_industry')->where('id', $data['id'])->update($data);
        }
        if($re){
            $this->success(__('添加成功'), 'jishu/hlist');
        }else{
            $this->success(__('添加失败'), 'jishu/hlist');
        }
    }

    public function hdelete(){
        $id=input('get.id');
        $re=Db::table('member_app_industry')->where('id',$id)->delete();
        if($re){
            $this->success(__('删除成功'), 'jishu/hlist');
        }else{
            $this->success(__('删除失败'), 'jishu/hlist');
        }
    }


    //应用部署
    public function slist(){
        $list=Db::table('member_app_deploy')->paginate(8);
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    public function sedit(){
        //应用语言选项
        $tech=Db::table('member_app_language')->select();
        $this->assign('tech',$tech);

        $id=input('get.id');
        $data=array();
        if($id){
            $data=Db::table('member_app_deploy')->where('id', $id)->find();
            $data['tech_id']=explode(',', $data['tech_id']);
        }
        $this->assign('data',$data);
        return $this->view->fetch();
    }

    public function ssave(){
        $data=$this->request->post();
        $data['tech_id']=implode(',', $data['tech_id']);
        if(empty($data['id'])){
            $re=Db::table('member_app_deploy')->insert($data);
        }else{
            $re=Db::table('member_app_deploy')->where('id', $data['id'])->update($data);
        }
        if($re){
            $this->success(__('添加成功'), 'jishu/slist');
        }else{
            $this->success(__('添加失败'), 'jishu/slist');
        }
    }

    public function sdelete(){
        $id=input('get.id');
        $re=Db::table('member_app_deploy')->where('id',$id)->delete();
        if($re){
            $this->success(__('删除成功'), 'jishu/slist');
        }else{
            $this->success(__('删除失败'), 'jishu/slist');
        }
    }



	//时间预算
	public function tlist(){
		$list=model('BudgetManage')->paginate(10);
	    $this->assign('list',$list);
		return $this->view->fetch();
	}
	public function tedit(){
        $id=input('get.id');
		$data=array();
		if($id){
          $data=model('BudgetManage')->where('id', $id)->find();
		}
		$version=model('AppVersion')->select();
		$cate=model('BudgetCate')->select();
		$this->assign('data',$data);
		$this->assign('version',$version);
		$this->assign('cate',$cate);
		return $this->view->fetch();
	}

	public function tsave(){
    	$data=$this->request->post();
    	$model=model('BudgetManage');
    	if(empty($data['id'])){
           $model->data($data);
    	   $re=$model->save();
    	}else{
    	   $re=$model->save($data,['id' => $data['id']]);
    	}
    	if($re!==false){
    		$this->success(__('编辑成功'), 'jishu/tlist');
    	}else{
    		$this->success(__('编辑失败'), 'jishu/tlist');
    	}
    } 


    public function tdelete(){
    	$id=input('get.id');
    	$re=model('BudgetManage')->destroy($id);
        if($re){
    		$this->success(__('删除成功'), 'jishu/tlist');
    	}else{
    		$this->success(__('删除失败'), 'jishu/tlist');
    	}
    }




	//预算分类
	public function tclist(){
		$list=model('BudgetCate')->paginate(10);
	    $this->assign('list',$list);
		return $this->view->fetch();
	}

	public function tcedit(){
        $id=input('get.id');
		$data=array();
		if($id){
          $data=model('BudgetCate')->where('id', $id)->find();
		}
		$this->assign('data',$data);
		return $this->view->fetch();
	}

	public function tcsave(){
    	$data=$this->request->post();
    	$model=model('BudgetCate');
    	if(empty($data['id'])){
           $model->data($data);
    	   $re=$model->save();
    	}else{
    	   $re=$model->save($data,['id' => $data['id']]);
    	}
    	if($re!==false){
    		$this->success(__('编辑成功'), 'jishu/tclist');
    	}else{
    		$this->success(__('编辑失败'), 'jishu/tclist');
    	}
    } 


    public function tcdelete(){
    	$id=input('get.id');
    	$re=model('BudgetCate')->destroy($id);
        if($re){
    		$this->success(__('删除成功'), 'jishu/tclist');
    	}else{
    		$this->success(__('删除失败'), 'jishu/tclist');
    	}
    }


    
    //应用逻辑 
	public function yylist(){
		$list=model('AppLogic')->paginate(10);
	    $this->assign('list',$list);
		return $this->view->fetch();
	}

	public function yyedit(){
		$id=input('get.id');
		$data=array();
		if($id){
          $data=model('AppLogic')->where('id', $id)->find();
		}
		$this->assign('data',$data);
		return $this->view->fetch();
	}


	public function yycsave(){
    	$data=$this->request->post();
    	$model=model('AppLogic');
    	if(empty($data['id'])){
           $model->data($data);
    	   $re=$model->save();
    	}else{
    	   $re=$model->save($data,['id' => $data['id']]);
    	}
    	if($re!==false){
    		$this->success(__('编辑成功'), 'jishu/yylist');
    	}else{
    		$this->success(__('编辑失败'), 'jishu/yylist');
    	}
    } 


    public function yydelete(){
    	$id=input('get.id');
    	$re=model('AppLogic')->destroy($id);
        if($re){
    		$this->success(__('删除成功'), 'jishu/yylist');
    	}else{
    		$this->success(__('删除失败'), 'jishu/yylist');
    	}
    }


}